from utils import query_utils
from utils.common_utils import json_response, Pagination
from utils.decorators import json_check, check_login
from user_app.models import AccountInfo, ModuleType, Role


@check_login
def get_module_type(request):
    """
        获取所有模块信息
    """
    module_list = ModuleType.objects.all().order_by("id")
    result_list = []
    for item in module_list:
        tmp = {
            "id": item.id,
            "module_name": item.module_name,
            "parent_id": item.parent_id,
            "is_show": item.is_show,
        }
        result_list.append(tmp)
    return json_response({"code": 0, "message": "", "data": {"item": result_list}})


@check_login
@json_check
def add_role(request, json_data: dict):
    """
        新增角色
    """
    role_name = json_data.get("role_name")
    module_list = json_data.get("module_list")
    is_superuser = json_data.get("is_superuser")
    if Role.objects.filter(role_name=role_name).exists():
        return json_response({"code": 1000, "message": "角色名重复"})
    if not module_list:
        return json_response({"code": 1000, "message": "权限配置不正确"})
    role = Role()
    role.role_name = role_name
    role.module_list = module_list
    if is_superuser == 2:
        # 标识为普通管理员能进入后台
        role.is_superuser = 2
    role.save()
    return json_response({"code": 0, "message": "添加成功"})


@check_login
def get_role_list(request):
    """
        角色列表接口
    """
    page = int(request.GET.get('page', "1"))
    per_page = int(request.GET.get("per_page", "10"))
    role_count = Role.objects.all().count()
    p = Pagination(page=page, per_page=per_page, total=role_count)
    role_list = Role.objects.all().order_by("id")[p.offset:p.offset + per_page]
    result_list = []
    for item in role_list:
        tmp = {
            "id": item.id,
            "role_name": item.role_name,
            "enable": item.enable,
            "created_on": item.created_on,
            "module_list": item.module_list,
            "module_name_list": query_utils.module_list_to_name(item.module_list)
        }
        result_list.append(tmp)
    return json_response({"code": 0, "message": "", "data": {"item": result_list, "page_info": p.dict}})


@check_login
def get_account_role_list(request):
    """
        所有用户角色列表
    """
    page = int(request.GET.get('page', "1"))
    per_page = int(request.GET.get("per_page", "10"))
    role = request.GET.get("role_id")
    is_allow_post = request.GET.get("is_allow_post")
    nick_name = request.GET.get("nick_name")
    query = AccountInfo.objects
    if role:
        query = query.filter(role_id=role)
    if is_allow_post:
        query = query.filter(is_allow_post=is_allow_post)
    if nick_name:
        query = query.filter(nick_name__icontains=nick_name)
    p = Pagination(page=page, per_page=per_page, total=query.count())
    account_list = query.order_by("id")[p.offset:p.offset + per_page]
    result_list = []
    for item in account_list:
        tmp = {
            "id": item.id,
            "nick_name": item.nick_name,
            "role_name": Role.objects.get(id=item.role_id).role_name,
            "role_id": item.role_id,
            "is_allow_post": "正常" if item.is_allow_post == 0 else "禁言",
            "create_time": item.create_time,
        }
        result_list.append(tmp)
    return json_response({"code": 0, "message": "", "data": {"item": result_list, "page_info": p.dict}})


@check_login
def band_account_role(request):
    """
        绑定用户角色
    """
    account_id = request.GET.get("account_id")
    role_id = request.GET.get("role_id")
    if account_id and role_id:
        if not Role.objects.filter(id=role_id).exists():
            return json_response({"code": 1000, "message": "角色未找到"})
        role_info = Role.objects.get(id=role_id)
        if role_info.is_superuser == 2:
            # 2为能进入后台管理的角色
            AccountInfo.objects.filter(id=account_id).update(role_id=role_id, is_superuser=2)
        else:
            AccountInfo.objects.filter(id=account_id).update(role_id=role_id, is_superuser=0)
        return json_response({"code": 0, "message": "修改成功"})
    else:
        return json_response({"code": 1000, "message": "参数错误"})


@check_login
def disable_account_post(request):
    """
        禁言用户接口
    """
    account_id = request.GET.get("account_id")
    account = AccountInfo.objects.get(id=account_id)
    result_dict = {

    }
    message = '禁言成功'
    if account.is_allow_post == 0:
        account.is_allow_post = 1
        result_dict['flag'] = True
    elif account.is_allow_post == 1:
        account.is_allow_post = 0
        result_dict['flag'] = False
        message = '取消禁言成功'
    account.save(update_fields=['is_allow_post'])
    return json_response({"code": 0, "message": message, "data": result_dict})
